﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Xml;

namespace GamePlugin.Logics.Data.Auction
{
    public class AuctionItemList : DependencyObject
    {
        public AuctionItemList()
        {
            Children = new ObservableCollection<AuctionItem>();
        }

        public void Sort()
        {
            List<AuctionItem> items = Children.ToList<AuctionItem>();
        }

        public static readonly DependencyProperty ChildrenProperty =
            DependencyProperty.Register("Children", typeof(ObservableCollection<AuctionItem>), typeof(AuctionItemList), null);
        public ObservableCollection<AuctionItem> Children
        {
            get { return (ObservableCollection<AuctionItem>)GetValue(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }

        public void ReadXml(XmlNode root)
        {
            Children.Clear();

            root = root.SelectSingleNode("./" + "Root");

            var countNode = root.SelectSingleNode("./" + "Count");
            int count = int.Parse(countNode.InnerText);

            for (int i = 0; i < count; i++)
            {
                var node = root.SelectSingleNode("./" + "Name" + i.ToString());
                AuctionItem item = new AuctionItem();
                item.ReadXml(node);
                item.LoadImageFromDisk();
                Children.Add(item);
            }

        }

        public void WriteXml(XmlWriter writer)
        {
            Sort();

            writer.WriteStartElement("Root");

            writer.WriteStartElement("Count");
            writer.WriteString(Children.Count.ToString());
            writer.WriteFullEndElement();

            for (int i = 0; i < Children.Count; i++)
            {
                writer.WriteStartElement("Name" + i.ToString());
                Children[i].WriteXml(writer);
                writer.WriteFullEndElement();
            }

            writer.WriteFullEndElement();
        }
    }
}
